package com.ming.closeto.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ming.closeto.R;
import com.ming.closeto.bean.SpecialBean;
import com.ming.closeto.utils.GlideUtils;
import com.ming.closeto.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class SpecialAdapter extends RecyclerView.Adapter<SpecialAdapter.ViewHolder> {

    public Context mContext;
    private LayoutInflater inflater;
    private List<SpecialBean> mList;

    private SpecialAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(SpecialAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public SpecialAdapter(Context mContext, List<SpecialBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.layout_special_rv_item, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        SpecialBean bean = mList.get(position);
        // 显示图片
        GlideUtils.loadImageviewCenterCrop(mContext, mList.get(position).getSmallUrl(), viewHolder.iv_img);
        // 显示文字
        viewHolder.tv_dowm_size.setText(bean.getDownloadSize());

        LogUtils.i("=======>" +  mList.get(position).getSmallUrl());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv_img;
        private TextView tv_dowm_size;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_dowm_size = itemView.findViewById(R.id.tv_down_size);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }
}
